<?php
require_once(dirname(__FILE__)."/../core/safeParam.php");
class Pages
{
  // ==================================================================
  // Add Page.
  function pagesAdd($vars)
  {
    global $database;
    $date             = $vars['date'];
    $title            = $vars['title'];
    $menu             = $vars['menu'];
    $description      = $vars['description'];
    $content          = $vars['content'];
    $linktitle        = $vars['linktitle'];
    $pagename         = str_replace(" ", "-", $linktitle);
    $rid              = $vars['rid'];
    $meta_title       = $vars['meta_title'];
    $meta_keywords    = $vars['meta_keywords'];
    $meta_description = $vars['meta_description'];
    $meta_manual      = $vars['meta_manual'];
    $parent           = $vars['parent'];
    if ($parent == '0') {
      $subcat = '0';
    } //$parent == '0'
    else {
      $subcat = '1';
    }
    if ($parent == '0') {
      $hassub = '0';
    } //$parent == '0'
    else {
      $hassub = '1';
      $query  = $database->update("pages", "hassub = '$hassub'", "id = '$parent'");
    }
    $query = $database->insert("pages", "date, title, linktitle, pagename, description, content, parent, subcat, hassub,menu,keywords, manualmeta,lastedit,lasteditdate, rid", "'$date', '$meta_title', '$linktitle', '$pagename', '$meta_description', '$content', '$parent', '$subcat', '$hassub', '$menu', '$meta_keywords', '$meta_manual', '" . $_SESSION['username'] . "', '" . date("Y-m-d H:i:s") . "', '$rid'");
    header('location: ../admin/index.php?edit=pages&updated=1');
  }
  function pagesAddLink($vars)
  {
    global $database;
    $date        = $vars['date'];
    $title       = $vars['title'];
    $menu        = $vars['menu'];
    $description = $vars['description'];
    $link        = $vars['link'];
    $status      = '2';
    $linktitle   = $vars['linktitle'];
    $pagename    = str_replace(" ", "-", $linktitle);
    $rid         = $vars['rid'];
    $parent      = $vars['parent'];
    if ($parent == '0') {
      $subcat = '0';
    } //$parent == '0'
    else {
      $subcat = '1';
    }
    if ($parent == '0') {
      $hassub = '0';
    } //$parent == '0'
    else {
      $hassub = '1';
      $query  = $database->update("pages", "hassub = '$hassub'", "id = '$parent'");
    }
    $query = $database->insert("pages", "date, title, linktitle, pagename, description, link, parent, subcat, hassub, status,menu,lastedit,lasteditdate,rid ", "'$date', '$title', '$linktitle', '$pagename', '$description', '$link', '$parent', '$subcat', '$hassub', '$status', '$menu', '" . $_SESSION['username'] . "', '" . date("Y-m-d H:i:s") . "', '$rid'");
    header('location: ../admin/index.php?edit=pages&updated=1');
  }
  // ==================================================================
  // Modify Page.
  function pagesEdit($vars)
  {
    global $database;
    $meta_title       = $vars['meta_title'];
    $meta_keywords    = $vars['meta_keywords'];
    $meta_description = $vars['meta_description'];
    $meta_manual      = $vars['meta_manual'];
    $id               = $vars['id'];
    $date             = $vars['date'];
    $menu             = $vars['menu'];
    $title            = $vars['title'];
    $description      = $vars['description'];
    $content          = $vars['content'];
    $linktitle        = $vars['linktitle'];
    $menuitem         = $vars['menuitem'];
    $parent           = $vars['parent'];
    $hassub           = $vars['hassub'];
    $pagename         = str_replace(" ", "-", $linktitle);
    if ($parent == '0') {
      $subcat = '0';
    } //$parent == '0'
    else {
      $subcat = '1';
    }
    $query = $database->update("pages", "date = '$date', title = '$meta_title', linktitle = '$linktitle', pagename = '$pagename', description = '$meta_description', content = '$content', parent = '$parent' , subcat = '$subcat', menu = '$menu', manualmeta = '$meta_manual', keywords = '$meta_keywords', lastedit = '" . $_SESSION['username'] . "', lasteditdate = '" . date("Y-m-d H:i:s") . "'", "id = '$id'");
    if (!$query) {
      echo "error".mysql_error();
      exit;
    } //!$query
    header('location: ../admin/index.php?edit=pages&updated=1');
  }
  function pagesEditLink($vars)
  {
    global $database;
    $id          = $vars['id'];
    $date        = $vars['date'];
    $menu        = $vars['menu'];
    $title       = $vars['title'];
    $description = $vars['description'];
    $content     = $vars['content'];
    $linktitle   = $vars['linktitle'];
    $menuitem    = $vars['menuitem'];
    $parent      = $vars['parent'];
    $hassub      = $vars['hassub'];
    $link        = $vars['link'];
    $pagename    = str_replace(" ", "-", $linktitle);
    if ($parent == '0') {
      $subcat = '0';
    } //$parent == '0'
    else {
      $subcat = '1';
    }
    $query = $database->update("pages", "date = '$date', title = '$title', linktitle = '$linktitle', pagename = '$pagename', description = '$description', link = '$link', parent = '$parent' , subcat = '$subcat', menu = '$menu', lastedit = '" . $_SESSION['username'] . "', lasteditdate = '" . time() . "'", "id = '$id'");
    if (!$query) {
      echo 'error';
      exit;
    } //!$query
    header('location: ../admin/index.php?edit=pages&updated=1');
  }
  // ==================================================================
  // 
  function pagesRemoveSub($id)
  {
    global $database;
    $query = $database->update("pages", "hassub = '0'", "id = '$id'");
    header('location: ../admin/index.php?edit=pages&updated=1');
  }
  // ==================================================================
  // Modify Position of page.
  function pagesOrder($vars)
  {
    global $database;
    $pos   = $vars['pos'];
    $posid = $vars['posid'];
    $query = $database->update("pages", "pos = '$pos'", "id = '$posid'");
    if (!$query) {
      echo 'error';
      exit;
    } //!$query
    header('location: ../admin/index.php?edit=pages&updated=1');
  }
  // ==================================================================
  // Delete a news article.
  function pagesDelete($vars)
  {
    global $database;
    $id     = $vars['id'];
    $parent = $vars['parent'];
    $query  = $database->query("SELECT * FROM pages WHERE parent = '$parent'");
    $rows   = mysql_num_rows($query);
    if ($rows == '1') {
      $query = $database->update("pages", "hassub = '0'", "id = '$parent'");
    } //$rows == '1'
    $query = $database->delete("pages", "id = '$id'");
    header('location: ../admin/index.php?edit=pages&updated=1');
  }
  // ==================================================================
  // 
  function pagesRemoveMenu($id)
  {
    global $database;
    $query = $database->update("pages", "menuitem = '0'", "id = '$id'");
    header('location: ../admin/index.php?edit=pages&updated=1');
  }
  // ==================================================================
  // 
  function pagesAddMenu($id)
  {
    global $database;
    $query = $database->update("pages", "menuitem = '1'", "id = '$id'");
    header('location: ../admin/index.php?edit=pages&updated=1');
  }
}
?>
